package com.hm.cms.auto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import com.hm.recycle.share.service.DbDocumentDeleteRecycleService;
import com.hm.recycle.share.service.DbDocumentNewRecycleService;
import com.hm.recycle.share.service.DbDocumentUpdateRecycleService;
import com.hm.system.share.service.mongo.log.RequestPlanLogService;
import com.hm.system.share.service.mongo.system.SysUserInfoService;

/**
 * @author shishun.wang
 * @date 下午4:58:02 2016年6月16日
 * @version 1.0
 * @describe
 */
@Configuration
@EnableConfigurationProperties({ CmsProperties.class })
public class CmsAutoConfiguration {

	@Autowired
	private CmsProperties cmsProperties;

	@SuppressWarnings("unused")
	@Autowired
	private BeanFactory beanFactory;
	
	@Bean
	public DbDocumentDeleteRecycleService dbDocumentDeleteRecycleService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(cmsProperties.getHttpInvokerService().getRecycle()
				+ DbDocumentDeleteRecycleService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(DbDocumentDeleteRecycleService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (DbDocumentDeleteRecycleService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public DbDocumentUpdateRecycleService dbDocumentUpdateRecycleService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(cmsProperties.getHttpInvokerService().getRecycle()
				+ DbDocumentUpdateRecycleService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(DbDocumentUpdateRecycleService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (DbDocumentUpdateRecycleService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public DbDocumentNewRecycleService dbDocumentNewRecycleService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(cmsProperties.getHttpInvokerService().getRecycle()
				+ DbDocumentNewRecycleService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(DbDocumentNewRecycleService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (DbDocumentNewRecycleService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public RequestPlanLogService requestPlanLogService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(
				cmsProperties.getHttpInvokerService().getSystem() + RequestPlanLogService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(RequestPlanLogService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (RequestPlanLogService) httpInvokerProxyFactoryBean.getObject();
	}
	
	@Bean
	public SysUserInfoService sysUserInfoService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(
				cmsProperties.getHttpInvokerService().getSystem() + SysUserInfoService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(SysUserInfoService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (SysUserInfoService) httpInvokerProxyFactoryBean.getObject();
	}

	/*@Bean
	public FilterRegistrationBean corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		CorsConfiguration config = new CorsConfiguration();
		config.setAllowCredentials(true);
		config.addAllowedOrigin("*");
		config.addAllowedHeader("*");
		config.addAllowedMethod("OPTIONS");
		config.addAllowedMethod("HEAD");
		config.addAllowedMethod("GET");
		config.addAllowedMethod("PUT");
		config.addAllowedMethod("POST");
		config.addAllowedMethod("DELETE");
		config.addAllowedMethod("PATCH");
		source.registerCorsConfiguration("/**", config);
		final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
		bean.setOrder(0);
		return bean;
	}*/

}
